package com.hui.tally;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import com.hui.tally.adapter.AddBookAdapter;
import com.hui.tally.db.AccountBean;
import com.hui.tally.db.BookBean;
import com.hui.tally.db.DBManager;
import com.hui.tally.utils.AddBookDialog;

import java.util.ArrayList;
import java.util.List;

public class AddBookActivity extends AppCompatActivity {

    GridView gridView;
    AddBookAdapter addBookAdapter;
    List<BookBean> bookList;
    BookBean bookBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_book);
        gridView = findViewById(R.id.add_gv);
        bookList = new ArrayList<>();
        addBookAdapter = new AddBookAdapter(this,bookList);
        gridView.setAdapter(addBookAdapter);
        bookBean =new BookBean();  //创建bookBean对象
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(AddBookActivity.this,bookList.get(i).getBookname()+"账本被选中了",Toast.LENGTH_LONG).show();
                //携带该item的bookBean跳转到MainActivity
                Intent intent =new Intent(AddBookActivity.this,MainActivity.class);
                intent.putExtra("bookBean",bookList.get(i));
                startActivity(intent);
            }
        });

        gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {  //点击了头布局
                    return false;
                }
                int pos = position;
                BookBean clickBean = bookList.get(pos);  //获取正在被点击的这条信息

                //弹出提示用户是否删除的对话框
                showDeleteItemDialog(clickBean);
                return true;
            }
        });
    }
    /* 弹出是否删除某一条记录的对话框*/
    private void showDeleteItemDialog(final  BookBean clickBean) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示信息").setMessage("您确定要删除这条记录么？")
                .setNegativeButton("取消",null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int click_id = clickBean.getId();
//                        //执行删除的操作
                        DBManager.deleteItemFrombooktbById(click_id);
                        bookList.remove(clickBean);   //实时刷新，移除集合当中的对象
                        addBookAdapter.notifyDataSetChanged();   //提示适配器更新数据
                    }
                });
        builder.create().show();   //显示对话框
    }
    // 当activity获取焦点时，会调用的方法
    @Override
    protected void onResume() {
        super.onResume();
        loadDBData();
    }

    private void loadDBData() {
        List<BookBean> list = DBManager.getBookList();
        bookList.clear();
        bookList.addAll(list);
        addBookAdapter.notifyDataSetChanged();
    }

    //addbook点击事件
    public void addbook(View view) {
        showaddbookdialog();
    }
    //添加账本弹窗
    private void showaddbookdialog() {
        final AddBookDialog dialog = new AddBookDialog(this);
        dialog.show();
        dialog.setDialogSize();
        dialog.setOnEnsureListener(new AddBookDialog.OnEnsureListener() {
            @Override
            public void onEnsure() {
                String msg = dialog.getEditText();
                int color;
                if (dialog.getColor() == 0){
                    color = R.color.dyellow;
                }else {
                    color = dialog.getColor();
                }
                if (!TextUtils.isEmpty(msg)) {
                    bookBean.setBookname(msg);
                    bookBean.setColorId(color);
                    DBManager.insertItemTobooktb(bookBean);
                    AddBookActivity.this.finish();
                    Intent i =new Intent(AddBookActivity.this,AddBookActivity.class);
                    startActivity(i);
                }
                dialog.cancel();
            }
        });
    }
}